package org.example.controller;

import org.example.utils.BusinessException;
import org.example.utils.Code;
import org.example.utils.Result;
import org.example.model.Sysrole;
import org.example.service.ResourcesService;
import org.example.utils.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//定义表现层控制器bean
@Controller
@ResponseBody
@RequestMapping("/books")
public class UserController {

    @Autowired
    private ResourcesService resourcesService;


    @GetMapping
    public Result save() {
        List<Sysrole> list = resourcesService.save("");
        return Result.success("查询成功", 200, list);
    }

    //模糊查询
    @GetMapping("/{rolename}")
    public Result save(@PathVariable String rolename) {
        List<Sysrole> list = resourcesService.save(rolename);
        return Result.success("查询成功", 200, list);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
//        if(id == 1){
//            throw new BusinessException(Code.BUSINESS_ERR,"请不要使用你的技术挑战我的耐性!");
//        }
//        //模拟系统异常，将可能出现的异常进行包装，转换成自定义异常
//        try{
//            int i = 1/0;
//        }catch (Exception e){
//            System.out.println("我是异常");
//            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时，请重试!",e);
//        }
        return Result.success("删除成功", 2000, resourcesService.delete(id));
    }

    @PostMapping
    public Result add() {
        System.out.println("添加成功");
        return Result.success("添加成功", 2000);
    }

    @PutMapping
    public Result update(@RequestBody Sysrole sysrole) {
        return Result.success("编辑成功", 2000);
    }

}
